//? 服务器接收请求并且取出请求携带的参数 

//! 引入第三方包 express
var express = require('express')

//! 实例化express
var app = express()

//! 设置静态托管文件夹
//@ app.use() 注册中间件
//@ express.static() express内置的一个静态资源托管的中间件
app.use(express.static('static'))

//2 顺序2
//! 在所有接口前设置处理post请求参数的中间件
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

//!定义返回给客户端的数据
var arr = [
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15929780085@qq.com', name: 'zh', phone: '15929780085', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '18092602390@qq.com', name: 'vrc', phone: '18092602390', pass: '1234', ingroup: '编辑组', isadmin: true, status: false
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: true
    },
    {
        email: '15291695373@qq.com', name: 'ljq', phone: '15291695373', pass: '1234', ingroup: '编辑组', isadmin: true, status: false
    },
]

//! 设置接口
app.get('/user/list', (req, res) => {
    //? 获取get类型请求的参数：req.query
    var { page, count } = req.query
    //? req  请求对象，包含了和本次请求相关的所有信息（请求地址，请求方式，携带的参数）

    //!  分页
    //! arr.slice(0,10)   取前10个 0-9
    var newArr = arr.slice((page - 1) * count, page * count)


    // console.log('收到了来自客户端的请求');
    // console.log(page, count);
    // console.log(req.query.page,req.query.count);

    //? res  响应对象，可以借助res向客户端返回信息（字符串，json格式数据，网页）
    // res.send({ code: 200, msg: '请求成功', list: arr })//? 通过send向客户端发送数据

    res.send({ code: 200, msg: '请求成功', list: newArr })

})//? 假设获取请求列表

//! 设置模糊搜索
app.get('/user/search', (req, res) => {
    //* 接收参数
    var { searchkey } = req.query
    //*根据参数从用户的name属性中进行过滤
    var arn = arr.filter((item) => {
        //* 写法一
        //* indexof找到了返回的是下标 没找到返回-1
        return item.name.indexOf(searchkey) != -1
        //* 写法二
        //* includes找到了返回true 没找到返回false
        // return item.name.includes(searchkey)== true
        //* 写法三 正则
        // return new RegExp(searchkey).test(item.name) 
    })
    //* 返回数据
    res.send({ code: 200, msg: '查询成功', list: arn })
})


app.post('/user/login', (req, res) => {
    //* 想要接收post类型请求的参数 必须提前设置能解析post请求参数的中间件 express.json 和 express.urlencoded

    var { phone, pass } = req.body

    //! 用来自客户端的数据做判断，判断在不在服务端的用户列表中
    var i = arr.findIndex((item, index) => {
        return item.phone == phone && item.pass == pass
    })
    //! 如果i不是-1 说明来自客户端的数据是合法的
    if (i != -1) {
        //登录成功时可以一并返回登录成功的用户信息，登录令牌 
        res.send({ code: 200, msg: '登录成功', userinfo: arr[i], token: '' })

    } else {
        res.send({ code: 400, mag: '登录失败' })

    }


    // console.log('收到了来自客户端的请求');

    //1 顺序1
    // console.log(req.body);
    // console.log(phone, pass);

    // res.send({ code: 200, msg: '登录成功'})

})


// app.put()

// app.delete()



//! 设置监听
app.listen(8090, () => {
    console.log('服务端运行在：http://localhost:8090');
})
